package jvm;

/**
 * synchronized解决可见性
 *
 * @author: cuihao
 * @create: 2020-06-26 23:17
 **/
public class Visibility {
    private static boolean flag = false;

    public static void main(String[] args) {
        new Thread() {
            @Override
            public void run() {
                while (true) {
                    System.out.println(flag);
                    if (flag) {
                        System.out.println("停止");
                        break;
                    }
                }
            }
        }.start();

        try {
            Thread.sleep(1000);
        } catch (Exception e) {
            e.printStackTrace();
        }

        new Thread() {
            @Override
            public void run() {
                flag = true;
                System.out.println("改了");
            }
        }.start();
    }
}
